<%@ page import="ru.niir.goodfil.ui.SecurityBean" %>
<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<%@ taglib prefix="t" uri="http://myfaces.apache.org/tomahawk" %>


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Развоз</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <script type="text/javascript">
        function reloadPage() {
            window.location.reload();
        }
    </script>

    <style type="text/css">
        .vert-tekst {
            -webkit-transform: rotate(-90deg);
            -moz-transform: rotate(-90deg);
            -o-transform: rotate(-90deg);
            -ms-transform: rotate(-90deg);
            filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
            font-size: 1.2em;
        }

        .rich-table-subheadercell {
            white-space: normal;
            height: 120px;
        }

        .rich-table-cell {
            height: 100px;
			align: center;
        }

        .rich-table-subheadercell-rotate {
            -webkit-transform: rotate(-90deg);
            -moz-transform: rotate(-90deg);
            -o-transform: rotate(-90deg);
            -ms-transform: rotate(-60deg);
            filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
            white-space: normal;
            height: 120px;
            width: 10px;
        }

        p {
            font-size: 1.2em;
        }
        p.vert-tekst{
            font-size: 1.2em;
        }
    </style>
</head>
<body>
<f:view>
    <a4j:form id="jsForm" prependId="false">
        <a4j:jsFunction name="refresh" action="#{StorageDelivery.reloadData}" limitToList="true"
                        reRender="trListForm"/>
        <a4j:jsFunction name="removeStatus" ajaxSingle="true" action="#{StorageDelivery.removeStorageDeliveryStatus}">
            <a4j:actionparam name="statusId" assignTo="#{StorageDelivery.statusId}"/>
        </a4j:jsFunction>

        <a4j:jsFunction name="updateStatus" ajaxSingle="true" action="#{StorageDelivery.updateStorageDeliveryStatus}">
            <a4j:actionparam name="statusId" assignTo="#{StorageDelivery.status}"/>
        </a4j:jsFunction>
    </a4j:form>
    <jsp:include page="status.jsp"/>
    <jsp:useBean id="StorageDelivery" class="ru.niir.goodfil.ui.StorageDeliveryListBean"/>
    <jsp:include page="StorageMenu.jsp"/>
    <h:form>
        <h:panelGroup>
            <h:commandLink value="Полная выгрузка"
                           action="#{StorageDelivery.currentStorageDeliveryListToExcel}"
                           style="margin-left: 10px; font-size: 1.5em"
                    />
            <h:commandLink value="Выгрузить в Excel и распечатать"
                           action="#{StorageDelivery.shortStorageDeliveryListToExcel}"
                           style="margin-left: 10px; font-size: 1.5em"/>
        </h:panelGroup>
    </h:form>

    <h:form id="dateForm">
        <h:panelGroup>
            <a4j:commandLink value="Обновить"
                             action="#{StorageDelivery.reloadData}"
                             reRender="trListForm, errorDate, deliveryInfoForm"
                             style="margin-left: 10px; font-size: 1.5em"/>
        </h:panelGroup>

<%--        <h:selectOneRadio value="#{StorageDelivery.deliveryType}" styleClass="radiobutton">
            <f:selectItems value="#{StorageDelivery.deliveryTypes}"/>
            <a4j:support action="#{StorageDelivery.reloadData}"
                         event="onclick"
                         reRender="trListForm"
                    />
        </h:selectOneRadio>--%>

        <div align="center">
            <h:panelGrid columns="5" cellpadding="10" cellspacing="10">
                <h:outputText value="Доставка заказов на: " styleClass="b"/>
                <rich:calendar value="#{StorageDelivery.selectedDate}" locale="RU_ru"
                               datePattern="dd.MM.yyyy" style="width:80px;padding-left:4px;" id="calBegdate"/>

                <a4j:commandButton value="Выбрать"
                                   action="#{StorageDelivery.reloadData}"
                                   reRender="dateForm, trListForm, message, deliveryInfoForm"
                                   style="width:150px;font-weight:bold;"/>
            </h:panelGrid>

        </div>
    </h:form>
    <h:form id="trListForm">
        <%if (StorageDelivery.isRenderDateMessage()) {%>
        <p id="message" style="color: #FF0000">Не сохранен лист развоза на
            <jsp:getProperty name="StorageDelivery" property="errorDate"/>
        </p>
        <%}%>
        <jsp:include page="messages.jsp"/>
        <div style="margin-top: 10px; height: 74%;" class="scroll_div">
            <rich:dataTable
                    id="trListTable"
                    value="#{StorageDelivery.model}"
                    var="item"
                    rowKeyVar="row"
                    styleClass="full"
                    >

                <rich:column>
                    <f:facet name="header">
                        <h:outputText value="№"/>
                    </f:facet>
                    <h:outputText value="#{row + 1}. "/>
                </rich:column>

               <rich:column rendered="#{SecurityBean.isRoleAdministrator}"
                        >
                    <f:facet name="header">
                        <h:outputText value=""/>
                    </f:facet>
                    <a4j:commandLink onclick="removeStatus(#{item.id})"
                                     title="Удалить"
                                     oncomplete="refresh();"
                                     reRender="trListForm, trListTable"
                                     immediate="true"
                                     >
                        <h:graphicImage value="/resources/img/delete.gif"/>
                    </a4j:commandLink>
                </rich:column>
                <rich:column filterBy="#{item.order.client.companyName}"
                             filterEvent="onblur"
                             sortable="true"
                             sortBy="#{item.order.client.companyName}"
                             styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             headerClass="rich-table-subheadercell">
                    <f:facet name="header">
                        <h:outputText value="Наименование компании"/>
                    </f:facet>
                    <p><h:outputText value="#{item.order.client.companyName }"/></p>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             headerClass="#{SecurityBean.isRoleKassa ? '':'rich-table-subheadercell-rotate'}"
                             rendered="#{!SecurityBean.isRoleStoreman}">
                    <f:facet name="header">
                        <h:outputText value="Дата создания"/>
                    </f:facet>
                    <h:panelGroup rendered="#{!SecurityBean.isRoleKassa}">
                        <p class="vert-tekst"><h:outputText value="#{item.order.creationDateAsString}"/></p>
                    </h:panelGroup>
                    <p><h:outputText value="#{item.order.creationDateAsString}" rendered="#{SecurityBean.isRoleKassa}"/></p>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             headerClass="#{SecurityBean.isRoleKassa ? '' : 'rich-table-subheadercell'}"
                             rendered="#{!SecurityBean.isRoleKassa}">
                    <f:facet name="header">
                        <h:outputText value="Объем, м3/Вес, кг"/>
                    </f:facet>
                    <p>
                        <h:panelGrid columns="1">
                            <h:outputText value="#{item.order.orderVolume}">
                                <f:convertNumber pattern="#0.00"/>
                            </h:outputText>
                            <h:outputText value="#{item.order.orderWeight}">
                                <f:convertNumber pattern="#0.00"/>
                            </h:outputText>
                        </h:panelGrid>
                    </p>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             headerClass="#{SecurityBean.isRoleKassa ? '' : 'rich-table-subheadercell-rotate'}"
                             >
                    <f:facet name="header">
                        <h:outputText value="Менеджер, тел."/>
                    </f:facet>
                    <h:panelGroup rendered="#{!SecurityBean.isRoleKassa}">
                        <p class="vert-tekst"><h:outputText value="#{item.order.manager.fio} "/>
                            <h:outputText value="#{item.order.manager.telephoneNumber}"/></p>
                    </h:panelGroup>
                    <p> <h:outputText value="#{item.order.manager.fio}" rendered="#{SecurityBean.isRoleKassa}"/>
                        <h:outputText value="#{item.order.manager.telephoneNumber}" rendered="#{SecurityBean.isRoleKassa}"/></p>
                </rich:column>

                <rich:column sortable="true"
                             sortBy="#{item.order.sum}"
                             styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}">
                    <f:facet name="header">
                        <h:outputText value="Сумма"/>
                    </f:facet>
                    <p><h:outputText value="#{item.order.factSumAsString}"/></p>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             headerClass="#{SecurityBean.isRoleKassa ? '' : 'rich-table-subheadercell'}">
                    <f:facet name="header">
                        <h:outputText value="№ ИС"/>
                    </f:facet>
                    <h:panelGroup rendered="#{!SecurityBean.isRoleKassa}">
                        <p class="vert-tekst"><h:outputText value="#{item.order.c1Num}"/></p>
                    </h:panelGroup>
                    <p><h:outputText value="#{item.order.c1Num}" rendered="#{SecurityBean.isRoleKassa}"/></p>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             style="#{SecurityBean.isRoleKassa ? '' : 'width: 25%;'}"
                        >
                    <f:facet name="header">
                        <h:outputText value="№1С"/>
                    </f:facet>
                    <h:inputTextarea value="#{item.documentNum1C}"
                                     style="padding: 0px; width: 98%; height: 98%; font: 12px;"
                                     readonly="#{!StorageDelivery.readOnly || (item.saved && SecurityBean.isRoleStoreman)}"
                                     cols="30"
                                     rendered="#{!SecurityBean.isRoleKassa}"/>
                    <h:inputTextarea value="#{item.documentNum1C}"
                                     style="padding: 0px; width: 98%; height: 98%; font: 12px;"
                                     cols="30"
                                     rendered="#{SecurityBean.isRoleKassa}"/>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             style="#{SecurityBean.isRoleKassa ? 'width: 30%;' : 'width: 15%'}"
                             headerClass="#{SecurityBean.isRoleKassa ? '' : 'rich-table-subheadercell'}">
                    <f:facet name="header">
                        <h:outputText value="Информация для водителей"/>
                    </f:facet>
                    <p><h:outputText value="#{item.order.driverInfo}"/></p>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}" sortable="true" sortBy="#{item.deliveryState}">
                    <f:facet name="header">
                        <h:outputText value="Доставка"/>
                    </f:facet>
                    <h:selectOneMenu value="#{item.deliveryState}" styleClass="f_s"
                                     style="width:70px" disabled="#{!StorageDelivery.readOnly || (item.saved && SecurityBean.isRoleStoreman)}">
                        <f:selectItems value="#{StorageDelivery.storageDeliveryStateList}"/>
                    </h:selectOneMenu>
                    <h:outputText value="#{item.order.factDeliveryDateAsString}" rendered="#{!item.order.factDeliveryDateNull}" styleClass="loadingDate"/>
                </rich:column>

                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}"
                             style="width: 30%"
                             rendered="#{!SecurityBean.isRoleKassa}">
                    <f:facet name="header">
                        <h:outputText value="Комментарий склада"/>
                    </f:facet>
                    <h:inputTextarea value="#{item.comment}"
                                     readonly="#{!StorageDelivery.readOnly || (item.saved && SecurityBean.isRoleStoreman)}"
                                     style="padding: 0px; width: 98%; height: 98%; font: 12px;"
                                     validatorMessage="Поле комментарий должно быть не менее 5 символов"
                                     required="#{item.deliveryState == 1}"
                                     requiredMessage="Поле \"Комментарий\" обязательно для заполнения если заказ не доставлен"
                            >
                        <f:validateLength minimum="5"/>
                    </h:inputTextarea>
                </rich:column>
                <rich:column styleClass="#{item.date > item.order.deliveryDate ? 'delivery':''}" rendered="#{!SecurityBean.isRoleKassa}">
                    <f:facet name="header">
                        <h:outputText value="Водитель-экспедитор"/>
                    </f:facet>
                    <h:selectOneMenu value="#{item.driverId}" styleClass="f_s" disabled="#{!StorageDelivery.readOnly || (item.saved && SecurityBean.isRoleStoreman)}" rendered="#{item.order.deliveryTypeId == 2 || item.order.deliveryTypeId == 4}">
                        <f:selectItems value="#{StorageDelivery.drivers}"/>
                    </h:selectOneMenu>
                    <h:outputText value="#{item.driver.carNumber}" rendered="#{item.driverSet}" styleClass="loadingDate"/>
                </rich:column>
                <rich:column rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman}" >
                    <f:facet name="header">
                        <h:outputText value="Действия"/>
                    </f:facet>
                    <a4j:commandButton value="Сохранить"
                                       action="#{StorageDelivery.updateStorageDeliveryStatus}"
                                       disabled="#{!StorageDelivery.readOnly || (item.saved && SecurityBean.isRoleStoreman)}"
                                       reRender="trListTable">
                        <f:setPropertyActionListener value="#{item}" target="#{StorageDelivery.status}"/>
                                       </a4j:commandButton>
                </rich:column>
                <rich:column rendered="#{SecurityBean.isRoleKassa}">
                    <f:facet name="header">
                        <h:outputText value="Касса"/>
                    </f:facet>
                    <a4j:commandButton value="Сохр. док-ты"
                                       action="#{StorageDelivery.updateStorageDeliveryStatusByKassa}"
                                       reRender="trListTable">
                        <f:setPropertyActionListener value="#{item}" target="#{StorageDelivery.status}"/>

                    </a4j:commandButton>
                </rich:column>
            </rich:dataTable>
        </div>
    </h:form>
    <h:form id="deliveryInfoForm">
        <rich:panel>
        <f:facet name="header">
            <h:outputText value="ДОСТАВЛЕНО"/>
        </f:facet>
        <h:panelGrid cellpadding="0" cellspacing="0" columns="6" columnClasses="dleft,dright">
            <h:outputText value="Вес" styleClass="b"/>
            <h:outputText value="#{StorageDelivery.delivery.weight}"/>
            <h:outputText value="Объем" styleClass="b"/>
            <h:outputText value="#{StorageDelivery.delivery.volume}"/>
            <h:outputText value="Сумма" styleClass="b"/>
            <h:outputText value="#{StorageDelivery.delivery.sumAsString}"/>
        </h:panelGrid>
    </rich:panel>
        <rich:panel>
            <f:facet name="header">
                <h:outputText value="НЕ ДОСТАВЛЕНО"/>
            </f:facet>
            <h:panelGrid cellpadding="0" cellspacing="0" columns="6" columnClasses="dleft,dright">
                <h:outputText value="Вес" styleClass="b"/>
                <h:outputText value="#{StorageDelivery.notDelivery.weight}"/>
                <h:outputText value="Объем" styleClass="b"/>
                <h:outputText value="#{StorageDelivery.notDelivery.volume}"/>
                <h:outputText value="Сумма" styleClass="b"/>
                <h:outputText value="#{StorageDelivery.notDelivery.sumAsString}"/>
            </h:panelGrid>
        </rich:panel>
    </h:form>
</f:view>
</body>
</html>
